<template>
	<view>
		<view class="status_bar"> </view>
		<uni-nav-bar dark :fixed="true" background-color="#ff90af" :title="title" />
		<view class="logo-bar">
			<image class="logo" src="/static/aile.png"></image>
		</view>
		<uni-card>
			<uni-forms ref="customForm" :modelValue="formData" :rules="customRules">
				<uni-forms-item label="用户名" required name="username">
					<uni-easyinput v-model="formData.username" placeholder="用户名,邮箱或手机号码" />
				</uni-forms-item>
				<uni-forms-item label="密码" required name="password">
					<uni-easyinput type="password" v-model="formData.password" placeholder="请输入登录密码" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit">提交</button>
		</uni-card>
		<view class="logo-bar text-company">
			<text class="company">{{company}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '用户登录',
				sConf: getApp().globalData,
				company: 'XXX科技有限公司',
				formData: {
					username: getApp().globalData.username,
					password: getApp().globalData.password
				},
				customRules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '用户名不能为空'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空'
						}]
					}
				}
			}
		},
		methods: {
			submit() {
				this.$refs.customForm.validate().then(res => {
					//console.log('表单数据信息：', res);
					this.doLogin(res);
				}).catch(err => {
					//console.log('表单错误信息：', err);
				})
			},
			doLogin: function(e) {
				var that = this;
				const requestTask = uni.request({
					url: that.sConf.host + '/user/LoginUser/',
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					data: e,
					success: function(res) {
						// console.log('用户登录成功: ');
						// console.log(res.data);
						if (res.data.rescode == 200) {
							that.title = '登录成功';
							that.sConf.user = res.data.resmsg;
							that.sConf.auth = res.data.auth;
							that.sConf.token = res.data.token;
							uni.getStorage({
								key: 'pos',
								success: function(res) {
									that.sConf.pos = res.data
								}
							});
							uni.switchTab({
								url: '/pages/tap0/tap0'
							});
						} else {
							that.title = '登录失败';
						}
					},
					fail: function(err) {
						// console.warn('用户登录失败: ');
						// console.warn(err);
					}
				});
			}
		}
	}
</script>

<style>
	.logo-bar {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 100rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		border-radius: 15rpx;
	}

	.text-company {
		width: 100%;
		position: absolute;
		bottom: 80rpx;
		font-size: 24rpx;
		color: #8f8f94;
	}
</style>